#!/bin/bash
IFACE=$1
STATUS=$2
case "$STATUS" in
    up)
    logger -s "NM local-dns-prepender triggered by ${1} ${2}."
    DNS_IP="127.0.0.1"
    set +e
    logger -s "NM local-dns-prepender: Checking if local DNS IP is the first entry in resolv.conf"
    if grep nameserver /etc/resolv.conf | head -n 1 | grep -q "$DNS_IP" ; then
        logger -s "NM local-dns-prepender: local DNS IP already is the first entry in resolv.conf"
        exit 0
    else
        logger -s "NM local-dns-prepender: Looking for '# Generated by NetworkManager' in /etc/resolv.conf to place 'nameserver $DNS_IP'"
        sed -i "/^# Generated by.*$/a nameserver $DNS_IP" /etc/resolv.conf
    fi
    ;;
    *)
    ;;
esac
